package com.cs.study.homework.utils;

import java.io.*;

/**
 * @description:
 * @author: 景天
 * @date: 2024/3/4 14:34
 **/
/*
文件操作相关的工具类  都是静态的方法
 */
public class FileUtils {
    //// 单字节复制
    //public static void copySingleByte(FileInputStream in, FileOutputStream outputStream) {
    //
    //}
    //
    //// 批量复制
    //public static void copyMultiByte(FileInputStream in, FileOutputStream outputStream) {
    //
    //}
    //
    //
    //// 单字节复制
    //public static void copySingleByte2(BufferedInputStream in, BufferedOutputStream outputStream) {
    //
    //}
    //
    //// 批量复制
    //public static void copyMultiByte2(BufferedInputStream in, BufferedOutputStream outputStream) {
    //
    //}

    // 以抽象类型作为参数

    // 单字节复制
    public static void copySingleByte(InputStream in, OutputStream out) throws IOException{
        // 表示读取到的字节值
        int readData;
        // 边读边写
        while ((readData = in.read()) != -1) {
            // 将读取到的字节值写入文件中
            out.write(readData);
        }
    }

    // 批量复制
    public static void copyMultiByte(InputStream in, OutputStream out)throws IOException {
        // 表示读取到的字节的个数
        int readCount;
        byte[] bytes = new byte[1024];
        while ((readCount = in.read(bytes)) != -1) {
            // 将读取的字节全部写入文件
            out.write(bytes, 0, readCount);
        }
    }


    // 单字符复制
    public static void copySingleChar(Reader in, Writer out) throws IOException{
        // 表示读取到的字符值
        int readData;
        // 边读边写
        while ((readData = in.read()) != -1) {
            // 将读取到的字符值写入文件中
            out.write(readData);
        }
    }

    // 批量复制 字符数组
    public static void copyMultiChars(Reader in, Writer out)throws IOException {
        // 表示读取到的字符的个数
        int readCount;
        char[] chars = new char[1024];
        while ((readCount = in.read(chars)) != -1) {
            // 将读取的字符全部写入文件
            out.write(chars, 0, readCount);
        }
    }

    // 按文本行进行复制(文本文件复制)
    public static void copyFileByLine(BufferedReader br, BufferedWriter bw) throws IOException {
        String line = null;
        while ((line = br.readLine()) != null) {
            // 把读取到的一行数据写入到新的文件
            bw.write(line);
        }
    }
}
